﻿//-----------------------------------------------------------------------
// <copyright file="BookProcessingLogics.cs" company="SampleThreeTier">
//     Sample 3 tier application by Anurag.
// </copyright>
//-----------------------------------------------------------------------
namespace BusinessLogics
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using AutoMapper;
    using BusinessEntity;
    using BusinessLayerContracts;
    using DataAccessLayerData;
    using DataAccessLayerFactory;

    /// <summary>
    /// The book processing related business logics
    /// </summary>
    public class BookProcessingLogics : BookProcessingLogicsBase, IBookProcessingBusiness
    {
        /// <summary>
        /// Adds the book details.
        /// </summary>
        /// <param name="bookEntity">The book entity.</param>
        /// <returns>
        /// Whether the book was added successfully
        /// </returns>
        public bool AddBookDetails(BookEntity bookEntity)
        {
            if (null != bookEntity)
            {
                var booksDataAccessLogic = BookDataAccessLayerFactory.GetBookProcessingDataInstance();
                var bookData = Mapper.Map<BookEntity, BookData>(bookEntity);
                return booksDataAccessLogic.AddBookDetails(bookData);
            }

            return false;
        }

        /// <summary>
        /// Gets the book details.
        /// </summary>
        /// <returns>Get the details of the book</returns>
        public List<BookEntity> GetBookDetails()
        {
            var booksDataAccessLogic = BookDataAccessLayerFactory.GetBookProcessingDataInstance();
            var bookDataList = booksDataAccessLogic.GetBookDetails();
            if (null != bookDataList)
            {
                var bookEntitiesList = Mapper.Map<List<BookEntity>>(bookDataList);
                return bookEntitiesList;
            }

            return null;
        }

        /// <summary>
        /// Updates the book details.
        /// </summary>
        /// <param name="updatedBookDetails">The updated book details.</param>
        /// <returns>Whether the book was updated successfully</returns>
        public bool UpdateBookDetails(BookEntity updatedBookDetails)
        {
            if (null == updatedBookDetails)
            {
                return false;
            }

            var booksDataAccessLogic = BookDataAccessLayerFactory.GetBookProcessingDataInstance();
            var updatedBookData = Mapper.Map<BookData>(updatedBookDetails);
            return booksDataAccessLogic.UpdateBookDetails(updatedBookData);
        }

        /// <summary>
        /// Deletes the book details.
        /// </summary>
        /// <param name="bookId">The book identifier.</param>
        /// <returns>Whether the book was deleted successfully</returns>
        public bool DeleteBookDetails(int bookId)
        {
            if (bookId >= 0)
            {
                var booksDataAccessLogic = BookDataAccessLayerFactory.GetBookProcessingDataInstance();
                return booksDataAccessLogic.DeleteBookDetails(bookId);
            }

            return false;
        }

        /// <summary>
        /// Gets the particular book details.
        /// </summary>
        /// <param name="bookId">The book identifier.</param>
        /// <returns>The book details</returns>
        public BookEntity GetParticularBookDetails(int bookId)
        {
            if (bookId >= 0)
            {
                var booksDataAccessLogic = BookDataAccessLayerFactory.GetBookProcessingDataInstance();
                var bookData = booksDataAccessLogic.GetParticularBookDetails(bookId);
                var bookEntity = Mapper.Map<BookEntity>(bookData);
                return bookEntity;
            }

            return null;
        }
    }
}
